#include "cppTutorObserver.h"
#include "cppTutorSubject.h"

// Constructor/Destructor are not used in our implementation and this helps to keep the code generic. 
// However, there are possibilities to use it for some applications.
Subject :: Subject ()
{
}

Subject :: ~Subject ()
{
}

// Establishes the relationship between Subject and Observer by attaching the input Observer to the _observers list. 
void Subject::Attach (Observer* o) 
{
    _observers.push_back(o);
}

// Terminates the relationship between Subject and Observer by removing the specified Observer from the _observers list. 
void Subject::Detach (Observer* o) 
{
    int count = _observers.size();
    int i;

    for (i = 0; i < count; i++)
    {
        if(_observers[i] == o)
        {
            break;
        }
    }
    if(i < count)
    {
        _observers.erase(_observers.begin() + i);
    }
}

void Subject::Notify () 
{
    int count = _observers.size();

    for (int i = 0; i < count; i++)
    {
        (_observers[i])->Update(this);
    }
}